﻿<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="RequestWorkTicket.aspx.cs" Inherits="OpenRAMS.Staff.RequestWorkTicket" %>
<%@ Import Namespace="OpenRAMS.Model" %>

<!DOCTYPE html>
<script runat="server">
    void btnTicketRequest_Click(Object sender, EventArgs e)
    {
        WorkTicket newWorkTicket = new WorkTicket();
        newWorkTicket.workTicketCreator = User.Identity.Name.ToString();
        newWorkTicket.workTicketAllocatedTo = "mtest";  //This should be changed to username of head of maintenance dept

        //Fix apostrophes
        String titleFix = sumRequest.Text;
        titleFix = titleFix.Replace("'", @"''");
        newWorkTicket.workTicketTitle = titleFix;

        String descFix = detRequest.Text;
        descFix = descFix.Replace("'", @"''");
        descFix = descFix.Replace(Environment.NewLine, "<br/>");
        newWorkTicket.workTicketDesc = descFix;
        
        newWorkTicket.workTicketLocation = locRequest.SelectedValue;
        
        //now try to save the work ticket.
        int tryToSave = newWorkTicket.Save();
        if (tryToSave < 0)
        {
            lblError.Visible = true;
            lblError.Text = "Error occurred during save (Did you type the wrong thing?)";
        }
        else
        {
            Response.Redirect("staff.aspx");
        }
    }
</script>


<html>
<head id="Head1" runat="server">
    <title>OpenRAMS - Send Maintenance Ticket</title>
    <link rel="Stylesheet" type="text/css" href="../Include/default.css" />
    <style type="text/css">
        .style1
        {
            width: 220px;
        }
    </style>
    <%
        //Outputs logged-in username in string
        lblUserName.Text = User.Identity.Name.ToString();
    %>
</head>
<body>
    <table class="tableDefault" align="center" width="90%">
    <tr><td><div class="tableTitle" align="center">Send Maintenance Ticket</div></td></tr>
    <tr>
    <td>
    <div class="tableBody">
        <form id="frmTicketRequest" runat="server">
        <table width="100%">
            <tr><td colspan="2" align="center"><asp:Label ID="lblError" Visible="false" runat="server" ForeColor="Red" /></td></tr>
        <tr>
        <td>Requester:</td>
        <td><asp:Label ID="lblUserName" Text="UserID" runat="server" Font-Bold="true"/></td>
        </tr>
        <tr>
        <td>Summary:</td>
        <td><asp:TextBox ID="sumRequest" runat="server" width="100%" Text="Type summary of work needed here." MaxLength="49"/></td>
        </tr>
        <tr>
        <td>Description:</td>
        <td width="80%"><asp:TextBox ID="detRequest" runat="server" TextMode="MultiLine" width="100%" Height="100px" Text="Type bigger description of work here." MaxLength="149" /></td>
        </tr>
        <tr>
        <td>Location</td>
        <td><asp:DropDownList ID="locRequest" runat="server" width="100%" 
                DataSourceID="SqlDataSource1" DataTextField="txtRoom" DataValueField="txtRoom"/>
            <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
                ConnectionString="<%$ ConnectionStrings:openramsConnectionString2 %>" 
                SelectCommand="SELECT [txtRoom] FROM [tblRooms]"></asp:SqlDataSource>
            </td>
        </tr>
        <tr>
        <td colspan="2">
        <table width="100%">
        <tr>
            <td width="50%"><asp:Button ID="btnTicketRequest" Text="Submit" runat="server" Width="100%" OnClick="btnTicketRequest_Click"/></td>
        <td width="50%"><asp:Button ID="btnTicketCancel" Text="Cancel" onClick="btnCancel_Click" runat="server" Width="100%"/></td>
        </tr>
        </table>
        </td>
        </tr>
        </table>
        </form>
    </div>
    </td>
    </tr>
    <tr>
    <td colspan="2"><div class="footer">
    <% 
        Response.WriteFile(Server.MapPath("~/Include/footer.txt"));
    %>
    </div></td>
    </tr>
    </table>
</body>
</html>
